package com.kt.mes.util;


import com.kt.mes.mapper.OrderProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 流水号生成工具
 */

@Component
public class SerialNumUtil {
    @Autowired
    private OrderProductMapper orderProductMapper;
    //静态变量存储最大值
    private static final AtomicInteger atomicNum = new AtomicInteger();

    @PostConstruct
    public void initMaxNum(){
        Map<String, Object> map=  orderProductMapper.findNumOrderOneDay();

        int num= Integer.valueOf(map.get("sum_order").toString()) ;
        atomicNum.set(num);
    }
    public String getNewAutoNum(){
        //线程安全的原子操作，所以此方法无需同步
        int newNum = atomicNum.incrementAndGet();
        //数字长度为5位，长度不够数字前面补0
        String newStrNum = String.format("%03d", newNum);
        return newStrNum;
    }
}
